﻿#region using

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

#endregion

namespace JupiterLibrary
{
    public class Particle
    {
        public Vector2 Position;
        public Color Color;
        public Vector2 Direction;
        /// <summary>
        /// Live in seconds
        /// </summary>
        public float Live;
        public float Aceleration;
        public float LiveInit;
        public Sprite Sprite;
        public int Cuadrante;

        public Particle()
        {
            Sprite = new Sprite();
        }
    }

    public class ParticleEmitter
    {
        public Texture2D Texture;

        public List<Particle> Particles;

        public ParticleEmitter(int numParticles)
        {
            Particles = new List<Particle>();

            Texture = Jupiter.Content.Load<Texture2D>("Textures/flare");

            for (int i = 0; i < numParticles; i++)
            {
                Particle particle = new Particle();

                Particles.Add(particle);
            }
        }

        public void Update()
        {
            float d=Jupiter.At1Second(1);
            
            foreach (Particle particle in Particles)
            {
                if (particle.Live < 0) continue;
                
                particle.Position+=particle.Direction*particle.Aceleration;

                particle.Sprite.SetRectangle(new Rectangle((int)particle.Position.X, (int)particle.Position.Y, 16, 16));
                particle.Sprite.SetColor(particle.Color);
                particle.Sprite.SetAlpha(particle.Live / particle.LiveInit);
                particle.Sprite.Texture = Texture;
                particle.Sprite.Update();

                particle.Live -= d;

            }
        }

        public void UpdateRectangle(Rectangle rectangle)
        {
            float d = Jupiter.At1Second(1);

            foreach (Particle particle in Particles)
            {
                if (particle.Live < 0) continue;

                if(particle.Cuadrante==0)
                {
                    particle.Position.X = rectangle.X;
                    particle.Position.Y = rectangle.Y;
                    particle.Cuadrante = 1;
                }

                if(particle.Cuadrante==1)
                {
                    particle.Direction = new Vector2(1, 0);

                    if (particle.Position.X > rectangle.Right) particle.Cuadrante++;
                }

                if (particle.Cuadrante == 2)
                {
                    particle.Direction = new Vector2(0, 1);

                    if (particle.Position.Y > rectangle.Bottom) particle.Cuadrante++;
                }

                if (particle.Cuadrante == 3)
                {
                    particle.Direction = new Vector2(-1, 0);

                    if (particle.Position.X < rectangle.X) particle.Cuadrante++;
                }
                if (particle.Cuadrante == 4)
                {
                    particle.Direction = new Vector2(0, -1);

                    if (particle.Position.Y < rectangle.Y) particle.Cuadrante=0;
                }
                particle.Position += particle.Direction * particle.Aceleration;

                particle.Sprite.SetRectangle(new Rectangle((int)particle.Position.X, (int)particle.Position.Y, 8, 8));
                particle.Sprite.SetColor(particle.Color);
                particle.Sprite.SetAlpha(particle.Live / particle.LiveInit);
                particle.Sprite.Texture = Texture;
                particle.Sprite.Update();

                particle.Live -= d;

            }
        }

        public void Draw()
        {
            Jupiter.Video.GraphicsDevice.BlendState = BlendState.Additive;

            foreach(Particle particle in Particles)
            {
                if (particle.Live < 0) continue;

                particle.Sprite.Draw();
            }
        }
    }
}
